/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#ifndef _estream_h
#define _estream_h

#include "edefs.h"
#include "etypes.h"

/*
 * Types
 */
struct estream {
    euint8_t	*ptr;
    eint32_t	 length;
    eint32_t	 offset;
    eint32_t	 fileno;

    eint32_t	 s_read		: 1;
    eint32_t	 s_write	: 1;
    eint32_t	 s_append	: 1;
    eint32_t	 s_nonblock	: 1;
    eint32_t	 s_buffered	: 1;
    eint32_t	 s_regular	: 1;
    eint32_t	 s_r_mode	: 1;
    eint32_t	 s_w_mode	: 1;
    /* don't wrap read buffer, must not be set when calling eseek or ewrite */
    eint32_t	 s_r_lock	: 1;
    eint32_t	 s_source	: 1;

#if HAVE_THREAD
    pthread_mutex_t	mutex;
#endif

    evector_t	*name;
    eint32_t	 line;
};

/*
 * Prototypes
 */
extern void
efdopen(eobject_t *pointer, int fileno, eint32_t mode);

extern void
efopen(eobject_t *pointer, evector_t *path, eint32_t mode);

extern off_t
eseek(estream_t *stream, off_t position, eint32_t whence);

extern ssize_t
eread(estream_t *stream, void *data, size_t count);

extern ssize_t
ewrite(estream_t *stream, void *data, size_t count);

extern ebool_t
eflush(estream_t *stream);

extern eint32_t
egetc(estream_t *stream);

extern void
eungetc(estream_t *stream, eint32_t ch);

extern eint32_t
eputc(estream_t *stream, eint32_t ch);

extern eint32_t
eclose(estream_t *stream);

extern void
echeck_buffer(estream_t *stream, eint32_t bytes);

/* don't wrap read buffer while lock is held */
extern void
eread_lock(estream_t *stream);

/* allow wrap of read buffer again */
extern void
eread_unlock(estream_t *stream);

extern void
init_stream(void);

/*
 * Externs
 */
extern estream_t *std_input, *std_output, *std_error;

#endif /* _estream_h */
